import type { Profile } from '@/types/user'
import { getLocalToken, removeLocalToken, setLocalToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref} from 'vue'

export const useUserStore = defineStore('hm-user', () => {
  // token信息
  const token = ref(getLocalToken() || '')

// 个人信息
const profile = ref<Profile>({} as Profile)

  // 设置用户信息 
  const setProfile = (val: Profile) => {
    profile.value = val
  }
  // 清空用户信息
  const clearProfile = () => {
    profile.value = {} as Profile
  }


  // 登录成功，保存token
  const setToken = (val:string) => {
    token.value = val
    // 持久化存储
    setLocalToken(val)
  }
// 退出登录，清除token
  const clearToken=()=>{
  token.value=''
  // 删除持久化
  removeLocalToken()
}
// 记得返回
  return {
    token,
    setToken,
    clearToken,
    profile,
    setProfile,
    clearProfile,
  }
})
